import React, { useEffect, useState } from 'react';
import './styles/editnotice.scss'
import { Button, Form, Input, notification, Select, DatePicker } from 'antd';
import { useLocation, useNavigate } from 'react-router-dom';
import * as service from "../../../api/Statistics"
interface Props {

}
function SystemEditNotice(props: Props) {
    const navigate = useNavigate()
    var location = useLocation()
    const [form] = Form.useForm()
    let time = new Date()

    useEffect(() => {
        if (location.state.type == "edit") {
            form.setFieldsValue(location.state.row)
        }
        console.log(location.state.row);//回显的数据
    }, [])
    const layout = {
        labelCol: { span: 8 },
        wrapperCol: { span: 16 },
    };
    // const handleChange = ((values: any) => {
    //     console.log(values);//下拉接收人
    //     setKind(values)
    // })
    const onFinish = (values: any) => {
        console.log(values);
        // console.log(values.endingtime);
        if (location.state.type == "edit") {
            console.log(values);
            service.notice_update({
                noticeId: location.state.row.noticeId,
                noticeName: values.noticeName,
                noticeContent: values.noticeContent,
                // accept: values.accept,
                journalName: values.journalName,
                noticeState: true,
                noticeTime: time,

                // noticeTime: location.state.row.noticeTime,
                // noticeEdit: time,
            }).then((res) => {
                console.log(res);
                notification.success({
                    placement: "top",
                    message: "编辑成功！"
                })
                navigate("/index/system-notice")
            })
        } else {
            service.notice_add({
                noticeName: values.noticeName,
                noticeContent: values.noticeContent,
                // accept: values.accept,
                journalName: values.journalName,
                noticeState: true,
                noticeTime: time,
            }).then((res) => {
                console.log(res);
                notification.success({
                    placement: "top",
                    message: "新增成功！"
                })
                navigate("/index/system-notice")
            })
        }


    };
    const back = () => {
        navigate(-1)
    }
    return (
        <div className='editnotice'>
            <h4>新增公告</h4>
            <h5>通知信息</h5>
            <Form
                {...layout}
                form={form}
                name="nest-messages"
                onFinish={onFinish}
                style={{ maxWidth: 600 }}
            >
                <Form.Item name="noticeName" label="消息标题">
                    <Input placeholder='请输入...' />
                </Form.Item>
                <Form.Item name="noticeContent" label="消息内容" >
                    <Input.TextArea placeholder='请输入...' />
                </Form.Item>
                {/* <Form.Item name="accept" label="接收人" >
                    <Select
                        style={{ width: 120 }}
                        // onChange={handleChange}
                        defaultValue="请选择"
                        options={[
                            { value: '李明明', label: '李明明' },
                            { value: '王汉文', label: '王汉文' },
                            { value: '李民进', label: '李民进' },
                        ]}
                    />
                </Form.Item> */}
                <Form.Item
                    label="发布状态"
                    name="noticeState"
                    hidden
                >
                    <Input placeholder='请输入' />
                </Form.Item>
                {
                    location.state.type == "edit" ?
                        <>
                            <Form.Item name="noticeTime" label="发布时间" >
                                <Input placeholder='请输入' style={{ width: 180 }} />
                            </Form.Item>
                        </>
                        : <></>

                }
                <Form.Item name="journalName" label="发布人" >
                    <Input placeholder='请输入...' style={{ width: 120 }} />
                </Form.Item>

                <Form.Item wrapperCol={{ ...layout.wrapperCol, offset: 8 }}>
                    <Button type="primary" htmlType="submit">
                        {
                            location.state.type == "edit" ? "保存" : "发布"
                        }

                    </Button>
                    <Button
                        onClick={() => { back() }} style={{ marginLeft: 20 }}>
                        返回
                    </Button>
                </Form.Item>
            </Form>
        </div>
    );
}

export default SystemEditNotice;